package com.wz.util;

import io.netty.handler.codec.base64.Base64Decoder;

import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.net.util.Base64;

public class StringUtil {
	/**sql日期格式*/
	public static String SQL_DATE = "yyyy-MM-dd HH:mm:ss";
	/**协议传输日期格式*/
	public static String PROTOCOL_DATE = "yyyyMMddHHmmss";

	/**
	 * 参数是否为空
	 * @param str
	 * @return
	 */
	public static boolean isNull(String... str){
		for (String string : str) {
			return string == null||("").equals(string.trim());
		}
		return false;
	}

	/**
	 * 是否是日期
	 * @param num 被验证的日期字符串
	 * @param fomat 日期格式
	 * @return
	 */
	public static boolean isDate(String fomat, String... dates){
		SimpleDateFormat dateFormat = new SimpleDateFormat(fomat);
		try {
			for (String date : dates) {
				if (isNull(date)){
					continue;
				}
				dateFormat.parse(date);
			}
			return true;
		} catch (ParseException e) {
			return false;
		}
	}

	/**
	 * 是否超出长度
	 * @param param
	 * @param size
	 * @return
	 */
	public static boolean overSize(int size,String... params){
		size = size<=0?20:size;
		for (String param : params) {
			if (isNull(param)){
				continue;
			}
			if(param.length()>size){
				return true;
			}
		}
		return false;
	}

	/**
	 * 制作发送内容
	 * @param connid
	 * @param data
	 * @param sign
	 * @return
	 */
	public static String getContent(String connid,String data){
		StringBuilder content = new StringBuilder();
		content.append("ConnId=").append(connid).append(" ");
		content.append("Data=").append(data).append(" ");
		return content.toString();
	}

	/**
	 * 扔掉内容中的app和cmd
	 * @param content
	 */
	public static String getContent(String content){
		try{
			String[] args = content.trim().split(" +");
			return args[2];
		} catch(IndexOutOfBoundsException e){
			return null;
		}
	}

	/**
	 * 默认缓冲区为150
	 * @param info
	 * @return
	 */
	public static String StringLinker(String... info){
		return StringLinker(150, info);
	}
	
	/**
	 * 定长的串字符
	 * @param info
	 * @return
	 */
	public static String StringLinker(int capacity,String... info){
		StringBuilder content = new StringBuilder(capacity);
		for (String i : info) {
			content.append(i);
		}
		return content.toString();
	}

  /**
   * 自定义格式取当前时间
   * @param format
   * @return
   */
	public static String getNow(String format){
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(new Date());
	}

	/**
	 * 得到当月第一天的日期
	 * 只支持sql格式
	 * @param date
	 * @return
	 */
	public static String getMothBeginDate(String date){
		return StringLinker(date.substring(0, 8),"01 00:00:00");
	}

	/**
	 * 屏蔽电话号码
	 * @param length
	 * @param mobNum
	 * @return
	 */
	public static String getMobileInfo(int length, String mobNum){
		char[] mob = mobNum.toCharArray();
		char star = "*".charAt(0);
		try {
			for(int i =3 ; i<3+length;i++){
				mob[i]= star;
			}
		} catch(ArrayIndexOutOfBoundsException e){
			throw new RuntimeException();
		}
		return String.valueOf(mob);
	}

	/**
	 * 返回几点几分
	 * 格式固定为 类似 2013-08-16 09:53:38,53
	 * @param dateTime
	 * @return
	 */
	public static String getChineseInfo(String dateTime){
		String time = dateTime.substring(11,16);
		String[] t = time.split("\\:");
		return StringLinker(t[0],"点",t[1],"分");
	}

	public static Map<String, String> getCookieParams(String value){
		Map<String, String> map = new HashMap<String, String>();
		String[] values = value.split(";");
		for (String str : values) {
			String[] s = str.split("=");
			map.put(s[0].trim(), s[1].trim());
		}
		return map;
	}


    public static String unicodeToString(String str) {
        Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
        Matcher matcher = pattern.matcher(str);
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt(matcher.group(2), 16);
            str = str.replace(matcher.group(1), ch + "");    
        }
        return str;
    }

	public static void main(String[] args) {
	}
	public static boolean isCurrentMonth(String dateTime){
		String month = dateTime.substring(5, 7);
		SimpleDateFormat dateFormat = new SimpleDateFormat("M");
		String currentMonth = dateFormat.format(new Date());
		return Integer.parseInt(month)==Integer.parseInt(currentMonth);
	}
}